package com.base.controller;


import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.voip.SelfShop;
import com.base.po.voip.SelfShopWithdrawals;
import com.base.po.voip.Users;
import com.base.service.LogsService;
import com.base.service.SelfShopOrderService;
import com.base.service.SelfShopService;
import com.base.service.SelfShopWithdrawalsService;
import com.base.service.UsersService;
import com.base.utils.IP;
import com.base.utils.Produce;

@Controller
@RequestMapping("/admin")
public class add_SelfShopWithdrawals {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private SelfShopService selfShopService;
	@Autowired
	private SelfShopOrderService selfShopOrderService;
	@Autowired
	private SelfShopWithdrawalsService selfShopWithdrawalsService;
	@Autowired
	private UsersService usersService;

	// 账户信息
	@RequestMapping("/add_SelfShopWithdrawals")
	public ModelAndView index(HttpServletRequest request, String selfshopid) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		if (adminUserId == null) {
			modelAndView.setViewName("jsp/login");
			return modelAndView;
		}
		
		Users users = usersService.selectByPrimaryKey(adminUserId);
		//可提现收益
		BigDecimal canCash = new BigDecimal(0);
		//提现记录
		List<SelfShopWithdrawals> selfShopWithdrawals = null;
		//店铺信息
		List<SelfShop> selfShopList = null;
		if(users.getRole() == 0 || users.getRole() == 1) {
			selfShopWithdrawals = selfShopWithdrawalsService.selectWithdrawals(adminUserId, adminUserId);
			if(selfShopWithdrawals != null && selfShopWithdrawals.size() > 0) {
				canCash = selfShopOrderService.sumCanCash(adminUserId, adminUserId, 
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(selfShopWithdrawals.get(0).getCreatetime()), 
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Produce.getNowDate()));
			}else {
				selfShopList = selfShopService.selectBySelfShop(adminUserId,adminUserId);
				canCash = selfShopOrderService.sumCanCash(adminUserId, adminUserId, 
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(selfShopList.get(0).getCreatetime()), 
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Produce.getNowDate()));
			}
		}
		if(users.getRole() == 2) {
			selfShopWithdrawals = selfShopWithdrawalsService.selectWithdrawals(users.getParentid(), adminUserId);
			if(selfShopWithdrawals != null && selfShopWithdrawals.size() > 0) {
				canCash = selfShopOrderService.sumCanCash(users.getParentid(), adminUserId, 
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(selfShopWithdrawals.get(0).getCreatetime()), 
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Produce.getNowDate()));
			}else {
				selfShopList = selfShopService.selectBySelfShop(users.getParentid(),adminUserId);
				canCash = selfShopOrderService.sumCanCash(users.getParentid(), adminUserId, 
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(selfShopList.get(0).getCreatetime()), 
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Produce.getNowDate()));
			}
		}
		
		request.setAttribute("canCash", canCash);
		request.setAttribute("selfshopid", selfshopid);
        
		modelAndView.setViewName("jsp/add_SelfShopWithdrawals");
		return modelAndView;
	}
	
	
	String SUCCESS = "success";

	// 添加商品
	@RequestMapping(value = "/insertSelfShopWithdrawals", method = RequestMethod.POST)
	public void insertSelfShopWithdrawals(HttpServletRequest request, HttpServletResponse response, 
			String name, String mobile, BigDecimal cash, String accountnum, String selfshopid,
			Integer type, BigDecimal canCash) throws IOException {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String adminId = (String) request.getSession().getAttribute("adminUserId");
		if (adminId == null) {
			out.print("请刷新页面重试！");
			return;
		}
		Users users = usersService.selectByPrimaryKey(adminId);
		
		if(cash.compareTo(canCash) == 1) {
			out.print("提现金额不能大于可提现金额");
			return;
		}
		
		List<SelfShopWithdrawals> selfShopWithdrawals = null;
		// 查询上一次提现申请 是否完成
		switch (users.getRole()) {
		case 0:
			selfShopWithdrawals = selfShopWithdrawalsService.selectWithdrawals(adminId, adminId);
			break;
        case 1:
        	selfShopWithdrawals = selfShopWithdrawalsService.selectWithdrawals(adminId, adminId);
			break;
        case 2:
        	selfShopWithdrawals = selfShopWithdrawalsService.selectWithdrawals(users.getParentid(), adminId);
	        break;
		default:
			break;
		}
		
		if(selfShopWithdrawals != null && selfShopWithdrawals.size() > 0) {
			if(selfShopWithdrawals.get(0).getStatus() == 1) {
				out.print("您有一条正在提现审核中的记录，请完成提现后再次申请");
				return;
			}
		}
		
		String id = Produce.getUUID(); 
		int resultCode = 0;
		if(users.getRole() == 0 || users.getRole() == 1) {
			resultCode = selfShopWithdrawalsService.insertSelective(new SelfShopWithdrawals(id,
					adminId, adminId, selfshopid, name, mobile, type, cash, accountnum,
					Produce.getNowDate(), (byte)1, (byte)1));
		}
		if(users.getRole() == 2) {
			resultCode = selfShopWithdrawalsService.insertSelective(new SelfShopWithdrawals(id,
					users.getParentid(), adminId, selfshopid, name, mobile, type, cash, accountnum,
					Produce.getNowDate(), (byte)1, (byte)1));
		}
		
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		if (resultCode == 1) {
			logsService.insert(CommContent.TYPE_UPDATE, users.getUsername()+"自建商城提现提交成功", usernamesys,
					true, IP.getIpAddr(request));
			out.print(SUCCESS);
		} else {
			logsService.insert(CommContent.TYPE_UPDATE, users.getUsername()+"自建商城提现提交失败", usernamesys,
					true, IP.getIpAddr(request));
			out.print("更改失败，请您重试");
		}
	}

}
